package com.wodan.zhongjinjicang.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.wodan.zhongjinjicang.common.exception.BusinessException;
import com.wodan.zhongjinjicang.common.redis.JedisClient;
import com.wodan.zhongjinjicang.common.utils.JsonUtils;
import com.wodan.zhongjinjicang.enums.BusinessErrorEnum;
import com.wodan.zhongjinjicang.frame.AbstractClientApiJsonBusinessMethodService;
import com.wodan.zhongjinjicang.mapper.UserMapper;
import com.wodan.zhongjinjicang.model.UserBean;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;



/**
 * 用户注销
 */
@Service("logout")
public class UserLogoutService extends AbstractClientApiJsonBusinessMethodService {


    @Autowired
    private JedisClient jedisClient;

    @Value("${REDIS_USER_SESSION_KEY}")
    private String REDIS_USER_SESSION_KEY;
    @Value("${SSO_SESSION_EXPIRE}")
    private Integer SSO_SESSION_EXPIRE;


    @Autowired
    private UserMapper userDao;


    @Override
    protected JSONObject doExecute(JSONObject data, String sessionId) {
        if(StringUtils.isBlank(sessionId)){
            throw new BusinessException(BusinessErrorEnum.ERROR.getCode(), "必须携带参数sessionId");
        }

        UserBean userBean = JsonUtils.jsonToPojo(data.toJSONString(), UserBean.class);

        //删除会话用户信息
        long del = jedisClient.del(REDIS_USER_SESSION_KEY + ":" + sessionId);
        if(del>0){
            return new JSONObject();
        }else{
            throw new BusinessException(BusinessErrorEnum.ERROR.getCode(), "注销失败");
        }

    }

    @Override
    protected boolean isNeedLogin() {
        return true;
    }

    @Override
    protected String[] getRequiredParams() {
        return new String[]{};
    }

}
